package org.rsbot.script.wrappers;

import java.awt.Point;
import java.awt.Rectangle;
import org.rsbot.bot.Bot;
import org.rsbot.client.input.Mouse;
import org.rsbot.script.Methods;

/**
 * This class handles the components of an interface. <br>
 * Can be used for banking/shops etc.
 * 
 * @author Qauters
 */
public class RSInterfaceComponent extends RSInterfaceChild {
    protected int componentIndex;
    private final Methods methods;
    private Mouse mouse;

    /**
     * @param parent
     * @param index
     * @param componentIndex
     */
    public RSInterfaceComponent(final RSInterface parent, final int index,
            final int componentIndex, final Methods methods) {
        super(parent, index, methods);
        this.methods = methods;
        this.componentIndex = componentIndex;
    }

    /**
     * The ID of this component
     * 
     * @return The ID of this component, or -1 if component == null
     */
    @Override
    public int getComponentID() {
        final org.rsbot.client.RSInterface component = getInterfaceInternal();
        if (component != null)
            return component.getComponentID();

        return -1;
    }

    /**
     * The index of this component
     * 
     * @return The index of this component, or -1 if component == null
     */
    public int getComponentIndex() {
        final org.rsbot.client.RSInterface component = getInterfaceInternal();
        if (component != null)
            return component.getComponentIndex();

        return -1;
    }

    /**
     * The name of this component
     * 
     * @return The name of this component, or "" if component == null
     */
    public String getComponentName() {
        final org.rsbot.client.RSInterface component = getInterfaceInternal();
        if (component != null)
            return component.getComponentName();

        return "";
    }

    /**
     * The stack size of this component
     * 
     * @return The stack size of this component, or -1 if component == null
     */
    @Override
    public int getComponentStackSize() {
        final org.rsbot.client.RSInterface component = getInterfaceInternal();
        if (component != null)
            return component.getComponentStackSize();

        return -1;
    }

    /**
     * Gets the client interface represented by this object.
     * 
     * @return The interface represented by this object.
     */
    @Override
    org.rsbot.client.RSInterface getInterfaceInternal() {
        final org.rsbot.client.RSInterface child = super.getInterfaceInternal();
        if (child != null) {
            final org.rsbot.client.RSInterface[] components = child
                    .getComponents();
            if ((components != null) && (componentIndex >= 0)
                    && (componentIndex < components.length))
                return components[componentIndex];
        }

        return null;
    }

    /**
     * Returns the center point of the Component. Written by Fusion89k.
     * 
     * @return The center point of the Component
     */
    public Point getPoint() {
        return new Point(getAbsoluteX() + getWidth() / 2, getAbsoluteY()
                + getHeight() / 2);
    }

    /**
     * Gets the absolute position of the child
     * 
     * @return the absolute position or new Point(-1, -1) if null
     */
    public Point getLocation() {
        return new Point(getAbsoluteX(), getAbsoluteY());
    }

    /**
     * Returns the center point of this interface
     * 
     * @return The center point of this interface
     */
    @Override
    public Point getCenter() {
        return new Point(getAbsoluteX() + getWidth() / 2, getAbsoluteY()
                + getHeight() / 2);
    }

    /**
     * Left-clicks this component.
     * 
     * @return <tt>true</tt> if the component was clicked.
     */
    public boolean click() {
        return click(true);
    }

    /**
     * Clicks this component.
     * 
     * @param leftClick <tt>true</tt> to left-click; <tt>false</tt> to
     *        right-click.
     * @return <tt>true</tt> if the component was clicked.
     */
    public boolean click(final boolean leftClick) {
        if (!isValid())
            return false;

        final Rectangle rect = getArea();
        if (rect.x == -1 || rect.y == -1 || rect.width == -1
                || rect.height == -1)
            return false;
        if (rect.contains(methods.getMouseLocation())) {
            methods.clickMouse(true);
            return true;
        }

        final int min_x = rect.x + 1, min_y = rect.y + 1;
        final int max_x = min_x + rect.width - 2, max_y = min_y + rect.height - 2;
        methods.clickMouse(methods.random(min_x, max_x, rect.width / 3),
                methods.random(min_y, max_y, rect.height / 3), leftClick);
        return true;
    }
}
